<template>
  <el-container>
    <my-theme-card>

      <el-main>
        <my-search-form :form-search="formSearch" @search="search" @addClick="addClick" />

        <template>
          <el-table :data="taskList" stripe border style="width: 100%" class="table" :header-cell-style="$store.state.settings.themeTableHeaderStyle">
            <el-table-column prop="procname" label="流程名称" width="180" />
            <el-table-column prop="procno" label="流程编号" />
            <el-table-column label="是否启用" width="180">
              <template slot-scope="scope">
                {{ key_logic[scope.row.useflg] }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="180">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
                <el-button type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
                <el-button type="text" size="small" @click="toPronode(scope.row)">节点信息</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>

      </el-main>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <my-delete-dialog
        :show-dialog="dialogConfirmVisible"
        :row="currentProcess"
        @deleteTask="deleteTask"
        @cancelTask="dialogConfirmVisible = false"
      />
    </my-theme-card>
  </el-container>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

<script>
import request from '@/utils/request'
import MyThemeCard from '../../../../components/MyThemeCard/index'
import MySearchForm from '../../../../components/MySearchForm'

export default {
  components: { MyThemeCard, MySearchForm },
  data: function() {
    return {
      formSearch: [
        {
          elType: 'select',
          field: 'useflg',
          dirField: 'logic',
          label: '是否启用',
          placeholder: '是否启用',
          daType: 'STRING',
          clearable: true,
          symbol: 'LIKE'
        }
      ],
      conditions: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      orders: [
      ],
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      taskList: [],
      currentProcess: {},
      dialogSourcesVisible: false,
      dialogConfirmVisible: false
    }
  },
  computed: {
    key_logic: function() {
      return this.$store.getters['directory/getDirectoryKeys']('logic')
    }
  },
  created() {
    this.$store.commit('directory/setDirectory', 'logic')
    this.query()
  },
  methods: {
    toPronode: function(row) {
      this.$router.push({ name: '流程节点', params: { procno: row.procno }})
    },
    editClick: function(row) {
      this.$emit('toChangePage', 'edit', row)
    },
    // 添加徽章点击事件
    addClick: function() {
      this.$emit('toChangePage', 'add')
    },
    deleteClick: function(row) {
      this.currentProcess = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/task/process/delete',
        method: 'post',
        data: this.currentProcess
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    // 搜索
    search: function() {
      this.page.current = 1
      this.query()
    },
    query: function() {
      this.$progress.start()
      this.page.conditions = this.conditions
      this.page.orders = this.orders
      request({
        url: '/task/process/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.taskList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
